/*<!-- saved from url=(0013)about:internet -->*/
//*** Initializations

var loca = new Array("North", "Center", "South", "Sharon", "Haifa", "Jerusalem", "Eilat");

//Adds an option to a select box
function addOption(selectbox,text,value)
{
        var optn = document.createElement("OPTION");
        optn.text = text;
        optn.value = value;
        selectbox.options.add(optn);
}

//Adds all appropriate field options
function addOptions_list() {
	
		//Setting styles per browser
	
		// IE
		if($.browser.msie||$.browser.mozilla) {
			
			//alert("I am IE!");
			$('head').append('"<link rel="stylesheet" type="text/css" href="../CSS/IE_regemployer.css" media="all" />"');
			$('head').append('<link rel="stylesheet" type="text/css" href="../CSS/buttonIE.css" media="all" />');
			
		}
		// Chrome, maybe FireFox too (need to test)
		
		if($.browser.mozilla) {
			
			//alert("I am IE!");
			$('head').append('"<link rel="stylesheet" type="text/css" href="../CSS/FI_regemployer.css" media="all" />"');
			$('head').append('<link rel="stylesheet" type="text/css" href="../CSS/buttonIE.css" media="all" />');
			
		}
		else
		{
			$('head').append('"<link rel="stylesheet" type="text/css" href="../CSS/CH_regemployer.css" media="all" />"');
			$('head').append('<link rel="stylesheet" type="text/css" href="../CSS/CH_button.css" media="all" />');
		}
		//Adding all location options
        for(var i=0; i< loca.length; ++i) {
            
            addOption(document.forms["myForm"]["location"], loca[i], loca[i]);
            
            }
		        
        $('#username_av').hide();
        $('#usernameerror').hide();
        $('#load').hide();
        

} 

//Function checks if a string contains any special characters (string is not a word)
//Returns - 'true' if special char was found in String (not word), 'false' - otherwise
function notAword(str)
{
        return /[^a-zA-Z]/.test(str);
}


//isOK = true - Form is filled out correctly
//isOK = false - Form is filled out incorrectly
var isOK = true;


//Company name validation
function val_fname() 
{

        var name=document.getElementById("compname").value;

        if (name==null || name=="")
        {
                document.getElementById("firstnameerror").style.visibility = "visible";
                isOK = false;
        }
 
        else
        {
                document.getElementById("firstnameerror").style.visibility = "hidden";
        }

}

//Company Field validation
function val_lname() 
{

        var compfield=document.getElementById("compfield").value;
        if (compfield==null || compfield=="")
        {
                document.getElementById("lastnameerror").style.visibility = "visible";
                isOK = false;
        }
        else if(notAword(compfield))
        {
                document.getElementById("lastnameerror").style.visibility = "visible";
                isOK = false;
        }
        else
        {
                document.getElementById("lastnameerror").style.visibility = "hidden";
        }


}

//Contact Name validation
function val_id()
{

	  var contactname=document.getElementById("contactname").value;
      if (contactname==null || contactname=="")
      {
              document.getElementById("iderror").style.visibility = "visible";
              isOK = false;
      }
      else if(notAword(contactname))
      {
              document.getElementById("iderror").style.visibility = "visible";
              isOK = false;
      }
      else
      {
              document.getElementById("iderror").style.visibility = "hidden";
      }

}

//E-mail validation
function val_email() 
{
        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; 
        var email=document.getElementById("email").value;
        var res=emailPattern.test(email);
        if(res==false)
        {       
                document.getElementById("emailerror").style.visibility = "visible";
                isOK = false;   
        }
        
        else
        {
                document.getElementById("emailerror").style.visibility = "hidden";
            	CheckAvailable();
        }

        
        
}

//Checks if a string is not a word or number (contains special chars except 'space')
//returns true -  If special characters exist
//returns false - If only letters or digits exist
function notWordOrNum(str) 
{       
        return /[^a-zA-Z0-9 ]/.test(str);
}

function val_cell()
{
	//cell validation
	var cell=document.getElementById("cell").value;

	if((cell.length>10) || ((cell.length>0) && (cell.length<10)))
	{
		document.getElementById("cellerror").style.visibility = "visible";
		isOK = false;
	}
	else if(isNaN(cell))
	{
		document.getElementById("cellerror").style.visibility = "visible";
		isOK = false;
	}
	
	else
	{
		document.getElementById("cellerror").style.visibility = "hidden";
	}

}


function CheckAvailable()
{		
		$('#username_av').hide(400);
		$('#usernameerror').hide(400);
		$('#load').show('slow');
		
		var email = $("#email").val(); 

        
        $.ajax({
        	  url: '../PHP/name_available.php?email='+email,
        	  success: function(data) {
        	    //$('.result').html(data);
        	   if(data=="OK") {
        		   $('#load').hide(400);
        		   document.getElementById("username_chars").style.visibility = "hidden";
        		  //$('#usernameerror').hide();
        	
        			setTimeout(function() { 
        				$('#username_av').show('slow');
        				
        			}, 450);
        	   }
        	   else if(data=="Error") {
        		   $('#load').hide(400);
        		   document.getElementById("username_chars").style.visibility = "hidden";
        		  // $('#username_av').hide();        
        		   setTimeout(function() { 
        				$('#usernameerror').show('slow');
        			}, 450);
        		  
        	   	}
        	   }
        	  });
}		

function val_pass()
{
	//Password validation
	var pass=document.getElementById("pass").value;
	if (pass==null || pass=="")
	{
		 	document.getElementById('passworderror').style.visibility = 'visible';  
	        isOK = false;
	}
	else
	{	
			document.getElementById('passworderror').style.visibility = 'hidden';  	
	}

}

function val_confpass()
{
	 //Password confirmation validation
	 var passconf=document.getElementById("passconfirm").value;
	 var pass = $("#pass").val();
	 if(passconf==null || passconf=="")
	 {
		 	document.getElementById('confpasserror').style.visibility = 'visible'; 
	  		isOK = false;
	 }

	 else if(pass!=passconf)
	 {
		 	document.getElementById('confpasserror').style.visibility = 'visible'; 
	        isOK = false;
	 }
	 else
	 {
		 document.getElementById('confpasserror').style.visibility = 'hidden'; 
	 }

}

function validateForm()
{
		//Initializing isOK flag
		isOK = true;
		
		//Checking all fields
        val_fname();
        val_lname();
        val_id();
        val_email();
        val_pass();
        val_confpass();
        
        
        if(isOK==false)
        {
        	alert("One or more of the detail fields are invalid","Invalid fields");
        	return false;
        }
        

}